﻿#include "mytabledata.h"
#include <QMessageBox>
#include <QDateTime>

MyTableData::MyTableData()
{
	m_PageNum=0;
	m_CurPageNo=0;

	m_pColumnInfo = NULL;
	m_pColBuffOffset = NULL;
	m_ShowFieidInfo = NULL;
	m_RecordNum = 0;

	m_CurAppType = 0;
	m_CurTableId = 0;
	m_CurColumnNum = 0;
	m_RecordLength = 0;
	m_PtlParamtype = 0;
	
	m_CurShowFieldNum = 0;
	ReleaseTableData();
	ReleaseTableParam();
	m_MenuInfo.ReleaseMenu();
	m_bModigyFlag = false;

}

MyTableData::~MyTableData()
{
	ReleaseTableData();
	ReleaseTableParam();
}

//初始化表参数
int MyTableData::InitTableParam()
{
	if (m_CurAppType<=0 || m_CurTableId <=0)
	{
		return -1;
	}
	//ReleaseTableParam();
	//m_CurTableId = TableId;
	//m_CurAppType = AppType;
	//m_CurParentId = ParentId;
	//m_PtlParamtype = PtlParamType;
	
	if (g_DbCfgFunObj.GetTableInfobyAppType_TableId(m_CurAppType, m_CurTableId, &m_CurTableInfo)<=0)
	{
		return -2;
	}

	InitColumnInfo(m_CurAppType, m_CurTableId);
	//m_MenuInfo.SetMenu(m_CurColumnNum, m_pColumnInfo, ParentId, TableId, AppType);
	InitShowFieldInfo();
	
	if (IsRecordParamSafe())
	{
		return 1;
	}else
	{
		return -3;
	}
}


//	初始化表数据
int MyTableData::InitTableData(int AppType, int TableId, int ParentId, int RecordId, unsigned char PtlParamType,int FUN)
{
	if (AppType<=0 || TableId <=0)
	{
		return -1;
	}
	if (AppType == m_CurAppType && 
		TableId == m_CurTableId &&
		ParentId == m_CurParentId &&
		RecordId == m_CurRecordId &&
		PtlParamType == m_PtlParamtype &&
		FUN == m_Fun_TotalShow_YX)
	{
		return ReInitTableData();
	}else
	{
		ReleaseTableParam();
		m_CurTableId = TableId;
		m_CurAppType = AppType;
		m_CurParentId = ParentId;
		m_CurRecordId = RecordId;
		m_PtlParamtype = PtlParamType;
		m_Fun_TotalShow_YX = FUN;

		InitTableParam();

		InitData();

		if (IsRecordParamSafe())
		{
			return 2;
		}else
		{
			return -3;
		}
	}
	return 0;
}

//	初始化表数据
int MyTableData::ReInitTableData()
{
	if (m_CurAppType<=0 || m_CurTableId <=0)
	{
		return -1;
	}
	//刷新一下记录
	InitData();
	if (IsRecordParamSafe())
	{
		return 1;
	}else
	{
		return -3;
	}

	return 0;
}

//初始化表数据
int MyTableData::InitData()
{
	if (m_CurAppType<=0 || m_CurTableId <=0)
	{
		return -1;
	}
	ReleaseTableData();
//	m_CurTableId = TableId;
//	m_CurAppType = AppType;
//	m_CurParentId = ParentId;
// 	if (g_DbCfgFunObj.GetTableInfobyAppType_TableId(AppType, TableId, &m_CurTableInfo)<=0)
// 	{
// 		return -2;
// 	}

	GetRecordData();
	m_MenuInfo.SetMenu(m_CurColumnNum, m_pColumnInfo, m_CurParentId, m_CurTableId, m_CurAppType);
//	InitColumnInfo(AppType, TableId);
//	m_MenuInfo.SetMenu(m_CurColumnNum, m_pColumnInfo, ParentId, TableId, AppType);
//	InitShowFieldInfo();
	
	if (IsRecordParamSafe())
	{
		return 1;
	}else
	{
		return -3;
	}
}


//读记录数据
int MyTableData::GetRecordData()
{
	if (m_CurTableId<=0 || m_CurAppType<=0 )
	{
		return -1;
	}

	m_PageNum=0;
	m_CurPageNo=0;
	m_RecordNum = 0;

//	qDebug() << QString::fromLocal8Bit("取前时间") <<QDateTime::currentDateTime();
	char	*buff = NULL;
	int	buff_len = 0;
	int	TableRecordLen =0;
//	m_RecordLength = 0;
	if (SYSTEM_TYPE_NSA_CD == m_CurAppType)
	{
		m_RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(m_CurAppType, m_CurTableId,buff, &buff_len, &TableRecordLen,true);
	}
	else if (m_CurParentId>0 )
	{
		m_RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(m_CurAppType, m_CurTableId,m_CurParentId, buff, &buff_len, &TableRecordLen,true);
	}
	else
	{
		if ( TABLE_RDEVS_INFO == m_CurTableId)
		{
			m_RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(m_CurAppType, m_CurTableId,m_CurParentId, buff, &buff_len, &TableRecordLen,true);
		}else if (m_CurTableId >= TABLE_RDEVS_YX && m_CurTableId <= TABLE_RDEVS_OUT)
		{
			
			
			m_RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_TotalShow(m_CurAppType, m_CurTableId,buff, &buff_len, &TableRecordLen,true,m_Fun_TotalShow_YX);
		}else
		{
			m_RecordNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(m_CurAppType, m_CurTableId,buff, &buff_len, &TableRecordLen,true);
		}
	}
//	qDebug() << QString::fromLocal8Bit("取后时间") <<QDateTime::currentDateTime();
	if (m_RecordNum>0 && buff && buff_len>0 && TableRecordLen>0)
	{
		for (int i=0; i<m_RecordNum; i++)
		{
			RecordInfo *pDataInfo = new RecordInfo;
			pDataInfo->RecordBuff = new char[m_RecordLength];
			memset(pDataInfo->RecordBuff, 0, m_RecordLength);
			pDataInfo->bNewFlag = false;
			pDataInfo->nRowAttrib = OPER_ROW_NOOPERATE;
			//pDataInfo->nDispRowIndex = i;
			pDataInfo->nRecordIndex = i;
			memcpy(pDataInfo->RecordBuff, buff+i*m_RecordLength, m_RecordLength);
			pDataInfo->BuffLength = TableRecordLen;
			pDataInfo->UpDateFieldlist.clear();
			m_RecordList.append(*pDataInfo);
		}

		SetPageNum();
	}
	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	if (TableRecordLen != m_RecordLength && TableRecordLen>0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("表记录长度与程序结构体长度不一致\r\n,表ID[%1], 应用类型[%2]").arg(m_CurTableId).arg(m_CurAppType));
		ReleaseTableData();
		return -1;
	}else
	{
		return 1;
	}
}

//	初始化表数据
int MyTableData::InitColumnInfo(int AppType, int TableId)
{
	int   ColRecordLen = 0;
	char  *buff = NULL;
	if (m_PtlParamtype>0)
	{
		g_DbCfgFunObj.GetPtlColumnInfobyAppType_PtlParamType(AppType, m_PtlParamtype, buff, &m_CurColumnNum);
	}else
	{
		g_DbCfgFunObj.GetColumnInfobyAppType_TableId(AppType, TableId, buff, &m_CurColumnNum);
	}
	
	if (buff && m_CurColumnNum>0)
	{
		m_pColumnInfo = (cColumn_Info *)buff;
		buff = NULL;
		ColRecordLen = 0;
		m_pColBuffOffset = new int[m_CurColumnNum];
		memset((char*)m_pColBuffOffset, 0, m_CurColumnNum*sizeof(int));
		for (int i=0; i<m_CurColumnNum; i++)
		{
			m_pColBuffOffset[i] = GetColumnDataOffset(i);
			ColRecordLen += m_pColumnInfo[i].clmDataLength;
		}
	}
	
	int   StructLen = GetTableRecordLen(AppType, TableId);
	if (StructLen != ColRecordLen /*|| (StructLen != m_RecordLength && m_RecordLength>0)*/ )
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("域名表记录长度与程序结构体长度不一致\r\n,表ID[%1], 应用类型[%2]").arg(TableId).arg(AppType));
		if (m_pColumnInfo)
		{
			delete [](char *)m_pColumnInfo;
			m_pColumnInfo = NULL;
		}

		if (m_pColBuffOffset)
		{
			delete [](int *)m_pColBuffOffset;
			m_pColBuffOffset = NULL;
		}
		return -1;
	}else
	{
		m_RecordLength = StructLen;
	}
// 	if (m_RecordLength<=0 )
// 	{
// 		m_RecordLength = ColRecordLen;
// 	}
	return m_RecordLength;
}


//	初始化显示字段的域信息
int MyTableData::InitShowFieldInfo()
{
	if (m_pColumnInfo && m_CurColumnNum>0 && m_pColBuffOffset)
	{
		int i = 0;
		int j = 0;
		int k = 0;
		m_CurShowFieldNum = 0;
		for (i=0; i<m_CurColumnNum; i++)
		{
			if (m_pColumnInfo[i].clmDisplayNo>0)
			{
				m_CurShowFieldNum++;
			}
		}

		if (m_CurShowFieldNum>0)
		{
			m_ShowFieidInfo = new FieldInfo_Show[m_CurShowFieldNum];
			
			memset((char*)m_ShowFieidInfo, 0, m_CurShowFieldNum*sizeof(FieldInfo_Show));
			for (j= 0; j<m_CurShowFieldNum; j++)
			{
				for (k=0; k<m_CurColumnNum; k++)
				{
					if (m_pColumnInfo[k].clmDisplayNo == j+1)
					{	
						m_ShowFieidInfo[j].ColumnNo = k;
						m_ShowFieidInfo[j].ColumnId = m_pColumnInfo[k].ColumnId;
						m_ShowFieidInfo[j].FieldNo = j;
						m_ShowFieidInfo[j].DataOffset = m_pColBuffOffset[k];
						break;
					}
				}
			}
			return 1;
		}
	}

	return -1;
}

//释放表数据
void MyTableData::ReleaseTableData()
{

	m_MenuInfo.ReleaseMenu();
	ReleaseRecordList();
	m_PageNum = 0;
	m_CurPageNo = 0;
	m_RecordNum = 0;
}

//释放表参数
void MyTableData::ReleaseTableParam()
{
	if (m_pColumnInfo)
	{
		delete [](char*)m_pColumnInfo;
		m_pColumnInfo = NULL;
	}
	if (m_pColBuffOffset)
	{
		delete [](int*)m_pColBuffOffset;
		m_pColBuffOffset = NULL;
	}
	if (m_ShowFieidInfo)
	{
		delete [](FieldInfo_Show *)m_ShowFieidInfo;
		m_ShowFieidInfo = NULL;
	}
	memset((char*)&m_CurTableInfo, 0, sizeof(cTable_Info));
	
	m_CurAppType = 0;
	m_CurTableId = 0;
	m_CurParentId = 0;
	m_RecordLength = 0;
	m_CurColumnNum = 0;	
	m_CurShowFieldNum = 0;
	//m_MenuInfo.ReleaseMenu();
	m_PageNum = 0;
	m_CurPageNo = 0;
	m_RecordNum = 0;
	
}


// 释放链表数据
void MyTableData::ReleaseRecordList()
{
	int ListCount = 0;
	ListCount = m_RecordList.count();
	if (ListCount>0)
	{
		for (int i=0; i<ListCount; i++)
		{
			m_RecordList[i].UpDateFieldlist.clear();
		}
		m_RecordList.clear();
	}
}

//	判断当前表的数据是否安全
bool MyTableData::IsRecordParamSafe()
{
	if (!m_pColumnInfo || !m_pColBuffOffset || 
		!m_ShowFieidInfo ||m_CurColumnNum <= 0 || 
		m_CurShowFieldNum <= 0 || m_CurAppType <=0 ||
		m_CurTableId <= 0)
	{
		return false;
	}
	return true;
}


int MyTableData::GetMenuInfoPoint(MenuInfo *&pMenuInfo)
{
	pMenuInfo = &m_MenuInfo;
	return 1;
}

int MyTableData::GetColumnDataOffset(int coluNo)
{
	int	BuffOffset = 0;
	if (coluNo>0 && coluNo<=m_CurColumnNum)
	{
		for (int i=0; i<coluNo; i++)
		{
			BuffOffset += m_pColumnInfo[i].clmDataLength;
		}
	}
	return BuffOffset;
}

int MyTableData::GetHeadText(int FieldIndex, QString &HeadText, int &ColWidth)
{
	HeadText.clear();
	ColWidth = 50;
	if (FieldIndex>=0 && FieldIndex <= m_CurShowFieldNum && 
		m_ShowFieidInfo && m_pColumnInfo)
	{
		int columnId = m_ShowFieidInfo[FieldIndex].ColumnId;
		int ColumnNo = m_ShowFieidInfo[FieldIndex].ColumnNo;
		if (ColumnNo>=0 && ColumnNo < m_CurColumnNum)
		{
			if (columnId == m_pColumnInfo[ColumnNo].ColumnId)
			{
				HeadText = QString::fromLocal8Bit(m_pColumnInfo[ColumnNo].ColumnChnName);
				ColWidth = m_pColumnInfo[ColumnNo].clmDisplayLenth;
				return 1;
			}
		}
	}

	return -1;
}

int MyTableData::GetHeadIcon(int FieldIndex, QString &IconPath)
{
	IconPath.clear();
	if (FieldIndex>=0 && FieldIndex <= m_CurShowFieldNum && 
		m_ShowFieidInfo && m_pColumnInfo)
	{
                unsigned int columnId = m_ShowFieidInfo[FieldIndex].ColumnId;
		int ColumnNo = m_ShowFieidInfo[FieldIndex].ColumnNo;
		if (ColumnNo>=0 && ColumnNo < m_CurColumnNum)
		{
			if (columnId == m_pColumnInfo[ColumnNo].ColumnId)
			{
				switch (m_pColumnInfo[ColumnNo].clmDisplayType)
				{
				case DISPLAY_TYPE_CHAR:
				case DISPLAY_TYPE_UCHAR:
				case DISPLAY_TYPE_SHORT:
				case DISPLAY_TYPE_USHORT:
				case DISPLAY_TYPE_INT32:
				case DISPLAY_TYPE_UINT32:
				case DISPLAY_TYPE_LONGLONG:
				case DISPLAY_TYPE_ULONGLONG:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/save.png");
					}
					break;
				case DISPLAY_TYPE_FLOAT:
				case DISPLAY_TYPE_DOUBLE:				
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/new.png");
					}
					break;
				case DISPLAY_TYPE_STRING:				//string
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				case DISPLAY_TYPE_BOOLEN:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				case DISPLAY_TYPE_MENU:
				case DISPLAY_TYPE_MULTI_MENU:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				case DISPLAY_TYPE_DATE:
				case DISPLAY_TYPE_TIME:
				case DISPLAY_TYPE_DATETIME:
				case DISPLAY_TYPE_TOTAL_TIME:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				case DISPLAY_TYPE_RELATED:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				case DIPPLAY_TYPE_COLOR:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				case DISPLAY_TYPE_PASSWORD:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				case DISPLAY_TYPE_HEX:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				case DISPLAY_TYPE_TABLE:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				case DISPLAY_TYPE_COLUMN:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				case DISPLAY_TYPE_EXP:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				default:
					{
						IconPath = QString::fromLocal8Bit(":/Res/images/open.png");
					}
					break;
				}
				return 1;
			}
		}

	}
	return 0;
}


int MyTableData::GetFieldData(int RecordNo, int FieldIndex, int ColumnNo, int ColumnId, char *&pDataBuff,char *&pRltDBuff, unsigned int &ShowFlag)
{
	pDataBuff = NULL;
	pRltDBuff = NULL;

	if (RecordNo<0 || RecordNo>=m_RecordNum ||
		FieldIndex<0 || FieldIndex>=m_CurShowFieldNum ||
		!IsRecordParamSafe())
	{
		return -1;
	}

	int upFieldListNo = 0;
	upFieldListNo = FindChangDataFromList(RecordNo, FieldIndex, ColumnId, ColumnNo);
	if (upFieldListNo>=0)
	{
		pDataBuff = m_RecordList[RecordNo].UpDateFieldlist[upFieldListNo].pDataBuff;
		//bChangeFlag = true;
		ShowFlag |= SHOW_FLAG_UNSAVE;
	}else
	{
		bool UpPublishedFlag = false;
		bool ErroFlag = false;
		int DataOffset = 0;
		DataOffset = GetFieldDataOffset(FieldIndex);
		if (DataOffset >=0 && DataOffset<m_RecordLength)
		{
			pDataBuff = m_RecordList[RecordNo].RecordBuff+DataOffset;
		}
		if (OPER_ROW_INSERT == m_RecordList[RecordNo].nRowAttrib)
		{
			ShowFlag |= SHOW_FLAG_UNSAVE;
			//bChangeFlag = true;
		}
	
// 		ErroFlag = CheckErrorFlag(m_RecordList[RecordNo].RecordBuff, ColumnId);
// 		if (ErroFlag)
// 		{
// 			ShowFlag |= SHOW_FLAG_ERROR;
// 		}
	}
	if (SYSTEM_TYPE_NSA_YD == m_CurAppType && 
		TABLE_RTASK_INFO == m_CurTableId && 
		0 == QString::fromLocal8Bit(m_pColumnInfo[ColumnNo].ColumnEngName).compare(QString::fromLocal8Bit("m_ProtType"),Qt::CaseInsensitive))
	{
		int RltFieldListNo = 0;
		RltFieldListNo = FindChangDataFromList(RecordNo, FieldIndex-1, ColumnId-1, ColumnNo-1);
		if (RltFieldListNo>=0)
		{
			pRltDBuff = m_RecordList[RecordNo].UpDateFieldlist[RltFieldListNo].pDataBuff;
		}else
		{
			int TempOffset = 0;
			TempOffset = GetFieldDataOffset(FieldIndex-1);
			if (TempOffset >=0 && TempOffset<m_RecordLength)
			{
				pRltDBuff = m_RecordList[RecordNo].RecordBuff+TempOffset;
			}
		}
	}
	if (pDataBuff)
	{
		return 1;
	}
	return 0;
}

//
// int MyTableData::GetFieldText(int RowNo, int FieldIndex, QVariant &FieldData, QString &FieldText,bool &bColorField, bool &bChangeFlag/*, QVariant FieldDataBefore*/)
// {
// 	if (RowNo<0 || RowNo>= TABLE_PAGE_ROW_NUM)
// 	{
// 		return -1;
// 	}
// 	int RecordNo = 0;
// 	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;
// 
// 	bColorField = false;
// 	bChangeFlag = false;
// 	FieldText.clear();
// 	if (RecordNo<0 || RecordNo>=m_RecordNum ||
// 		FieldIndex<0 || FieldIndex>=m_CurShowFieldNum ||
// 		!IsRecordParamSafe())
// 	{
// 		return -1;
// 	}
// 
// 	int DataOffset = 0;
// 	int columnId = 0;
// 	int columnNo = 0;
// 
// 	columnId = GetFieldColumnId(FieldIndex);
// 	if (columnId <=0)
// 	{
// 		return -2;
// 	}
// 	columnNo = GetColumnNoFromId(columnId);
// 
// 	DataOffset = GetFieldDataOffset(FieldIndex);
// 	int upFieldListNo = 0;
// 	char *pDataBuff = NULL;
// 	char *pRltBuff = NULL;
// 	upFieldListNo = FindChangDataFromList(RecordNo, FieldIndex, columnId, columnNo);
// 	if (upFieldListNo>=0)
// 	{
// 		pDataBuff = m_RecordList[RecordNo].UpDateFieldlist[upFieldListNo].pDataBuff;
// 		bChangeFlag = true;
// 	}else
// 	{
// 		if (OPER_ROW_INSERT == m_RecordList[RecordNo].nRowAttrib)
// 		{
// 			bChangeFlag = true;
// 		}
// 		pDataBuff = m_RecordList[RecordNo].RecordBuff+DataOffset;
// 	}
// 	if (RecordNo>=0 && RecordNo<m_RecordList.count())
// 	{
// // 		if (bChangeFlag && pDataBuff)
// // 		{
// // 			return m_DbTextObj.nGetDBText(pDataBuff, m_pColumnInfo+columnNo, &m_MenuInfo, FieldData, FieldText, bColorField, FieldDataBefore);
// // 		}else
// // 		{
// // 			bChangeFlag = false;
// // 			if (OPER_ROW_INSERT == m_RecordList[RecordNo].nRowAttrib)
// // 			{
// // 				bChangeFlag = true;
// // 			}
// 			//pDataBuff = m_RecordList[RecordNo].RecordBuff+DataOffset;
// 			return m_DbTextObj.nGetDBText(m_RecordList[RecordNo].RecordBuff, DataOffset,pDataBuff, m_pColumnInfo+columnNo, &m_MenuInfo, FieldData, FieldText, bColorField/*, FieldDataBefore*/);
// //		}
// 	}else
// 	{
// 		return -3;
// 	}
// }

//
int MyTableData::GetFieldText(int RowNo, int FieldIndex, QVariant &FieldData, QString &FieldText,bool &bColorField, unsigned int &ShowFlag)
{
	int RecordNo = 0;
	RecordNo = RowNo;

	ShowFlag = SHOW_FLAG_NORMAL;
	bColorField = false;
	//bChangeFlag = false;
	FieldText.clear();

	int   columnId = 0;
	int   columnNo = 0;
	char  *pDataBuff =NULL;
	char  *pRltBuff = NULL;
	columnId = GetFieldColumnId(FieldIndex);
	if (columnId <=0)
	{
		return -2;
	}
	columnNo = GetColumnNoFromId(columnId);

	GetFieldData(RecordNo, FieldIndex, columnNo, columnId, pDataBuff,pRltBuff, ShowFlag);
	if (pDataBuff)
	{
	//	return DBText::nGetDBText(pDataBuff, m_pColumnInfo+columnNo, &m_MenuInfo, FieldData, FieldText, bColorField, FielddataBefore);
		return m_DbTextObj.nGetDBText(pDataBuff,pRltBuff,m_pColumnInfo+columnNo, &m_MenuInfo, FieldData, FieldText, bColorField);
	}else
	{
		return -3;
	}

}

// int MyTableData::GetUpDateFieldText(int RowNo, int FieldIndex, QVariant &FieldData, QString &FieldText, bool &bColorField, QVariant FieldDataBefore)
// {
// 	if (RowNo<0 || RowNo>= TABLE_PAGE_ROW_NUM)
// 	{
// 		return -1;
// 	}
// 	int RecordNo = 0;
// 	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;
// 
// 
// 	bColorField = false;
// 	FieldText.clear();
// 	if (RecordNo<0 || RecordNo>=m_RecordNum ||
// 		FieldIndex<0 || FieldIndex>=m_CurShowFieldNum ||
// 		!IsRecordParamSafe())
// 	{
// 		return -1;
// 	}
// 
// 	int DataOffset = 0;
// 	int columnId = 0;
// 	int columnNo = 0;
// 	int upFieldListNo = 0;
// 	char *pDataBuff = NULL;
// 
// 	columnId = GetFieldColumnId(FieldIndex);
// 	if (columnId <=0)
// 	{
// 		return -2;
// 	}
// 	columnNo = GetColumnNoFromId(columnId);
// 
// 	DataOffset = GetFieldDataOffset(FieldIndex);
// 	upFieldListNo = FindChangDataFromList(RecordNo, FieldIndex, columnId, columnNo);
// 	if (upFieldListNo>=0)
// 	{
// 		pDataBuff = m_RecordList[RecordNo].UpDateFieldlist[upFieldListNo].pDataBuff;
// 	}
// 	if (!pDataBuff)
// 	{
// 		pDataBuff = m_RecordList[RecordNo].RecordBuff+DataOffset;
// 	}
// 	
// 	if (RecordNo>=0 && RecordNo<m_RecordList.count() && pDataBuff)
// 	{
// 		return m_DbTextObj.nGetDBText(pDataBuff, m_pColumnInfo+columnNo, &m_MenuInfo, FieldData, FieldText, bColorField, FieldDataBefore);
// 	}else
// 	{
// 		return -3;
// 	}
// 
// }

//获取域的数据类型
short MyTableData::GetFieldDataType(int FieldIndex)
{
	if (FieldIndex<0 || FieldIndex>=m_CurShowFieldNum || !IsRecordParamSafe())
	{
		return -1;
	}

	int columnId = 0;
	int columnNo = 0;

	columnId = GetFieldColumnId(FieldIndex);
	if (columnId <=0)
	{
		return -2;
	}
	columnNo = GetColumnNoFromId(columnId);
	short DataType = 0;
	if (columnNo>=0 && columnNo<m_CurColumnNum)
	{
		DataType = m_pColumnInfo[columnNo].clmDataType;
	}
	return DataType;
}

//	获取域显示类型
short MyTableData::GetFieldDisplayType(int FieldIndex)
{
	if (FieldIndex<0 || FieldIndex>=m_CurShowFieldNum ||
		!IsRecordParamSafe())
	{
		return -1;
	}

	int columnId = 0;
	int columnNo = 0;

	columnId = GetFieldColumnId(FieldIndex);
	if (columnId <=0)
	{
		return -2;
	}
	columnNo = GetColumnNoFromId(columnId);
	short DisplayType = 0;
	if (columnNo>=0 && columnNo<m_CurColumnNum)
	{
		DisplayType = m_pColumnInfo[columnNo].clmDisplayType;
	}
	return DisplayType;
}

//	获取域的关联是否是单关联（与对称关联相反）
bool MyTableData::GetFieldIsSingleRelate(int FieldIndex)
{
	if (FieldIndex<0 || FieldIndex>=m_CurShowFieldNum ||
		!IsRecordParamSafe())
	{
		return false;
	}

	int columnId = 0;
	int columnNo = 0;

	columnId = GetFieldColumnId(FieldIndex);
	if (columnId <=0)
	{
		return false;
	}
	columnNo = GetColumnNoFromId(columnId);
	char IsSingleRelate = 0;
	if (columnNo>=0 && columnNo<m_CurColumnNum)
	{
		IsSingleRelate = m_pColumnInfo[columnNo].clmIsbeSingleRelate;
	}
	if (SYS_STATE_VALID == IsSingleRelate)
	{
		return true;
	}
	return false;
}

//	获取域的关联信息
int MyTableData::GetFieldRelateAppType_TableId(int FieldIndex, int &AppType, int &TableId, bool &bSingleCheck)
{
	AppType = 0;
	TableId = 0;
	bSingleCheck = false;

	if (FieldIndex<0 || FieldIndex>=m_CurShowFieldNum ||
		!IsRecordParamSafe())
	{
		return -1;
	}

	int columnId = 0;
	int columnNo = 0;

	columnId = GetFieldColumnId(FieldIndex);
	if (columnId <=0)
	{
		return -2;
	}
	columnNo = GetColumnNoFromId(columnId);
	if (columnNo>=0 && columnNo<m_CurColumnNum)
	{
		AppType = m_pColumnInfo[columnNo].clmRltAppType;
		TableId = m_pColumnInfo[columnNo].clmRltTableId;
		bSingleCheck = m_pColumnInfo[columnNo].clmIsbeSingleRelate;
		return 1;
	}
	return 0;
}

//	获取域的信息
int MyTableData::GetFieldColumnInfo(int FieldIndex, cColumn_Info &ColumnInfo)
{
	if (FieldIndex<0 || FieldIndex>=m_CurShowFieldNum ||
		!IsRecordParamSafe())
	{
		return -1;
	}

	int columnId = 0;
	int columnNo = 0;

	columnId = GetFieldColumnId(FieldIndex);
	if (columnId <=0)
	{
		return -2;
	}
	columnNo = GetColumnNoFromId(columnId);
	if (columnNo>=0 && columnNo<m_CurColumnNum)
	{
		memset((char*)&ColumnInfo, 0, sizeof(cColumn_Info));
		memcpy((char*)&ColumnInfo, (char*)&(m_pColumnInfo[columnNo]), sizeof(cColumn_Info));
		return 1;
	}
	return 0;
}

//	获取当前域是否可编辑
void MyTableData::IsCurFieldEnableEdit(int FieldIndex, bool &bEdit, bool &bEdit_Once)
{
	bEdit = false;
	bEdit_Once = false;
	if (!IsRecordParamSafe())
	{
		return ;
	}
	if (IsCurTableEnableEdit(m_CurAppType, m_CurTableId, m_CurParentId))
	{
		int columnId = 0;
		int columnNo = 0;
		columnId = GetFieldColumnId(FieldIndex);
		if (columnId <=0)
		{
			return ;
		}
		columnNo = GetColumnNoFromId(columnId);
		if (columnNo<0 || columnNo>=m_CurColumnNum)
		{
			return ;
		}
		if (SYS_STATE_VALID == m_pColumnInfo[columnNo].clmIsInput)
		{
			bEdit = true;	
		}
		if (SYS_STATE_VALID == m_pColumnInfo[columnNo].clmIsOnlineChange)
		{
			bEdit_Once = true;	
		}
	}
}

bool MyTableData::IsCurFieldCombox(int FieldIndex)
{
	if (SYSTEM_TYPE_NSA_CD == m_CurAppType)
	{
		return false;
	}else
	{
		if (IsRecordParamSafe())
		{
			int columnId = 0;
			int columnNo = 0;
			columnId = GetFieldColumnId(FieldIndex);
			if (columnId <=0)
			{
				return false;
			}
			columnNo = GetColumnNoFromId(columnId);
			if (columnNo>=0 && columnNo<m_CurColumnNum)
			{
				if (SYS_STATE_VALID == m_pColumnInfo[columnNo].clmDisplayType)
				{
					return true;
				}
			}
		}
	}
	return false;
}

int	MyTableData::GetCurRecordNoByRowNo(int RowNo)
{
	int RecordNo = -1;
	if (RowNo<0 || RowNo>=TABLE_PAGE_ROW_NUM)
	{
		return -1;
	}
	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;

	if (RecordNo>= 0 && RecordNo< m_RecordList.count())
	{
		return RecordNo;
	}
	return -1;
}

//	获取当前行的记录ID
int MyTableData::GetCurRecordIdbyRowIndex(int RowNo, unsigned int &RecordId)
{
	if (RowNo<0 || RowNo>=TABLE_PAGE_ROW_NUM)
	{
		return -1;
	}
	int RecordNo = 0;
	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;

	RecordId = 0;
	char	*pDatabuff = NULL;
	if (RecordNo>= 0 && RecordNo< m_RecordList.count())
	{
		pDatabuff = m_RecordList.at(RecordNo).RecordBuff;
		if (pDatabuff)
		{
			memcpy(&RecordId, pDatabuff, sizeof(unsigned int));
			return 1;
		}
	}
	return -1;
}

//	获取当前行的记录ID
int MyTableData::GetCurRecordIdbyRowIndex(int RowNo, unsigned int &RecordId, bool &bNewRecord)
{
	if (RowNo<0 || RowNo>=TABLE_PAGE_ROW_NUM)
	{
		return -1;
	}
	int RecordNo = 0;
	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;

	RecordId = 0;
	bNewRecord = false;
	char	*pDatabuff = NULL;
	if (RecordNo>= 0 && RecordNo< m_RecordList.count())
	{
		pDatabuff = m_RecordList.at(RecordNo).RecordBuff;
		if (pDatabuff)
		{
			memcpy(&RecordId, pDatabuff, sizeof(unsigned int));
			if (OPER_ROW_INSERT == m_RecordList.at(RecordNo).nRowAttrib)
			{
				bNewRecord = true;
			}
			return 1;
		}
	}
	return -1;
}

//	获取当前域的数据偏移
int MyTableData::GetColumnNoFromId(unsigned int ColumnId)
{
	if (ColumnId>0 && ColumnId <= m_CurColumnNum && IsRecordParamSafe())
	{
		int columnNo = 0;
		if (ColumnId == m_pColumnInfo[ColumnId-1].ColumnId)
		{
			columnNo = ColumnId-1;
		}else
		{
			for (int i=0; i<m_CurColumnNum; i++)
			{
				if (ColumnId == m_pColumnInfo[i].ColumnId)
				{
					columnNo = i;
					break;
				}
			}
		}
		return columnNo;
	}else
	{
		return 0;
	}
}

//	获取新添加的记录个数
int MyTableData::GetNewInsertRows()
{
	if (m_RecordList.count()<=0)
	{
		return 0;
	}
	int NewRowsNum = 0;
	for (int Index=0; Index<m_RecordList.count(); Index++)
	{
		if (OPER_ROW_INSERT == m_RecordList[Index].nRowAttrib)
		{
			NewRowsNum++;
		}
	}

	return NewRowsNum;
}

//	检查是否可添加新记录
bool MyTableData::IsEnableAddNew()
{
	if (m_CurAppType<=0 || m_CurTableId <=0)
	{
		return false;
	}
	int	RecordNum = 0;
	QString str;
	if (g_DbCfgFunObj.GetRecordNumbyAppType_TableId(m_CurAppType, m_CurTableId, &RecordNum)<=0)
	{
		return false;
	}
	int newInsertNum = GetNewInsertRows();

	if (RecordNum + newInsertNum >= m_CurTableInfo.tabMaxRecordNum)						
	{					
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("表记录达到最大条数，不能再添加！"), QMessageBox::Ok | QMessageBox::Cancel);
		return false;
	}
	return true;
}

//	添加一条记录
int MyTableData::AddOneRecord(/*int RowNo*/)
{
	if (IsRecordParamSafe())
	{
		int RecordNo = 0;
		RecordNo = m_RecordList.count();

		RecordInfo *pDataInfo = new RecordInfo;
		pDataInfo->RecordBuff = new char[m_RecordLength];
		memset(pDataInfo->RecordBuff, 0, m_RecordLength);
		pDataInfo->bNewFlag = true;
		pDataInfo->nRowAttrib = OPER_ROW_INSERT;
		//pDataInfo->nDispRowIndex = RecordNo;
		pDataInfo->nRecordIndex = RecordNo;
		pDataInfo->BuffLength = m_RecordLength;
		pDataInfo->UpDateFieldlist.clear();
		g_DbCfgFunObj.SetDefaultData(pDataInfo->RecordBuff, m_pColumnInfo, m_pColBuffOffset, m_CurColumnNum, m_RecordLength, m_CurParentId);
		g_DbCfgFunObj.SetDefaultData_Special(m_CurAppType, m_CurTableId, pDataInfo->RecordBuff, RecordNo);
		m_RecordList.append(*pDataInfo);
		m_RecordNum = m_RecordList.count();
		SetPageNum();
		SetModifyFlag(true);
		return 1;
	}
	return 0;
}

//	设置记录数据的变化
int MyTableData::UpDateDataChang(int RowNo, int FieldIndex, QVariant ItemData)
{
	if (RowNo<0 || RowNo>=TABLE_PAGE_ROW_NUM)
	{
		return -1;
	}
	int RecordNo = 0;
	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;

	if (RecordNo<0 || RecordNo>= m_RecordList.count() || 
		FieldIndex <0 || FieldIndex>=m_CurShowFieldNum ||
		!IsRecordParamSafe())
	{
		return -1;		
	}
	//1、与原来的记录内存进行比较，
	//2、如果（1）结果相同，判断是否有修改项，有则删除修改项，无则返回
	//3、如果（1）结果不同，先判断是否是新插入记录，是 ： 则修改原记录，否：进一步判断是否有修改项，有则修改项，无新建修改项

	int	DataOffset = 0;
	int	columnId = 0;
	int	columnNo = 0;
	char	*pDataBuff = NULL;
	char	*pOldDataBuff = NULL;
	int	 buff_len = 0;
	bool	 bBuffSample = false;
//	bool	 bDelChangfield = false;

	columnId = GetFieldColumnId(FieldIndex);
	columnNo = GetFieldColumnNo(FieldIndex);
	if (columnId <=0 || columnNo<0 || columnNo>m_CurColumnNum)
	{
		return -2;
	}

	if (OPER_ROW_DELETE == m_RecordList[RecordNo].nRowAttrib)
	{
	//	qDebug() <<"删除标志，不修改";
		return 0;
	}

	DataOffset = GetFieldDataOffset(FieldIndex);
	if (RecordNo>=0 && RecordNo<m_RecordList.count())
	{
		if (m_RecordList[RecordNo].RecordBuff)
		{
			pOldDataBuff = m_RecordList[RecordNo].RecordBuff+DataOffset;
		}
	}

	m_DbTextObj.nGetDataBuff(pDataBuff, &buff_len, (cColumn_Info*)&m_pColumnInfo[columnNo], ItemData);

	if (!pDataBuff || buff_len<=0 || !pOldDataBuff)
	{
		return -3;
	}
	if (0 == memcmp(pOldDataBuff, pDataBuff, buff_len))
	{
		bBuffSample = true;
	//	qDebug() <<"前后相同，不修改";
	}else
	{
		bBuffSample = false;
	//	qDebug() <<"前后不相同，修改";
	}

	if (OPER_ROW_INSERT == m_RecordList[RecordNo].nRowAttrib && m_RecordList[RecordNo].bNewFlag)
	{
		if (!bBuffSample)
		{
			memcpy(pOldDataBuff, pDataBuff, buff_len);
		//	qDebug() <<"前后不相同，修改插入记录";
		}
	}else if (OPER_ROW_UPDATE == m_RecordList[RecordNo].nRowAttrib )
	{	
		if (bBuffSample)
		{
			int CfieldNo = FindChangDataFromList(RecordNo, FieldIndex, columnId, columnNo);
			if (CfieldNo>=0)
			{
				m_RecordList[RecordNo].UpDateFieldlist.removeAt(CfieldNo);
				if (m_RecordList[RecordNo].UpDateFieldlist.count()<=0)
				{
					m_RecordList[RecordNo].nRowAttrib = OPER_ROW_NOOPERATE;
				}
			//	qDebug() <<"与原记录相同，删除修改项";
			}
		}
		else
		{
			int CfieldNo = FindChangDataFromList(RecordNo, FieldIndex, columnId, columnNo);
			if (CfieldNo>=0)
			{
				if (m_RecordList[RecordNo].UpDateFieldlist[CfieldNo].pDataBuff)
				{
					if (0 != memcmp(m_RecordList[RecordNo].UpDateFieldlist[CfieldNo].pDataBuff, pDataBuff, buff_len))
					{
						memcpy(m_RecordList[RecordNo].UpDateFieldlist[CfieldNo].pDataBuff, pDataBuff, buff_len);
					//	qDebug() <<"前后不相同，修改";
					}
				}
			}else
			{
				AddChangeData(pDataBuff, buff_len, RecordNo,columnNo,FieldIndex);
				pDataBuff = NULL;
			//	qDebug() <<"新值，修改";
			}
		}
	}else if (OPER_ROW_NOOPERATE == m_RecordList[RecordNo].nRowAttrib)
	{
		if (!bBuffSample)
		{
			AddChangeData(pDataBuff, buff_len, RecordNo,columnNo,FieldIndex);
			pDataBuff = NULL;
		//	qDebug() <<"新值，修改";
		}
	}

	if (pDataBuff)
	{
		delete [](char*)pDataBuff;
		pDataBuff = NULL;
	}

	return 1;
}

//	添加一个数据更改项
int MyTableData::AddChangeData(char *&pDataBuff, int BuffLen, int RecordNo, int columnNo, int FieldIndex)
{	
	if (!pDataBuff || BuffLen<=0 || columnNo<0 || columnNo>m_CurColumnNum)
	{
		return -2;
	}
	if (RecordNo<0 || RecordNo>= m_RecordList.count() || 
		FieldIndex <0 || FieldIndex>=m_CurShowFieldNum ||
		!IsRecordParamSafe())
	{
		return -1;		
	}

	FieldData *pFieldData = new FieldData;
	pFieldData->pDataBuff = pDataBuff;
	pDataBuff = NULL;
	pFieldData->ColumnNo = columnNo;
	pFieldData->ColumnId = m_pColumnInfo[columnNo].ColumnId;
	pFieldData->Buff_Len = BuffLen;
	pFieldData->FieldIndex = FieldIndex;
	pFieldData->clmDataType = m_pColumnInfo[columnNo].clmDataType;
	m_RecordList[RecordNo].UpDateFieldlist.append(*pFieldData);
	if (OPER_ROW_UPDATE != m_RecordList[RecordNo].nRowAttrib)
	{
		m_RecordList[RecordNo].nRowAttrib = OPER_ROW_UPDATE;
	}
	SetModifyFlag(true);
	return 1;
}

//	查找数据更改是否已经有存在LIST
int MyTableData::FindChangDataFromList(int RecordNo, int FieldIndex, int columnId, int columnNo)
{
	int cFieldListNo = -1;
	if (RecordNo>=0 && RecordNo<m_RecordList.count() && 
		FieldIndex>=0 && FieldIndex<m_CurShowFieldNum &&
		columnId>0 && 
		columnNo>=0 && columnNo<m_CurColumnNum)
	{
		if (OPER_ROW_UPDATE == m_RecordList[RecordNo].nRowAttrib &&
			m_RecordList[RecordNo].UpDateFieldlist.count()>0)
		{
			for (int i=0; i<m_RecordList[RecordNo].UpDateFieldlist.count(); i++)
			{
				if (m_RecordList[RecordNo].UpDateFieldlist[i].FieldIndex == FieldIndex &&
					m_RecordList[RecordNo].UpDateFieldlist[i].ColumnId == columnId &&
					m_RecordList[RecordNo].UpDateFieldlist[i].ColumnNo == columnNo)
				{
					cFieldListNo = i;
					break;
				}
			}
		}
	}
	return cFieldListNo;
}


// 设置记录删除标志
int MyTableData::SetRecordDelete(int RowNo, bool bDeleteFlag, bool &bDelImmediately)
{
	if (RowNo<0 || RowNo>=TABLE_PAGE_ROW_NUM)
	{
		return -1;
	}
	int RecordNo = 0;
	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;

	bDelImmediately = false;
	if (!IsRecordParamSafe() || RecordNo<0 || RecordNo>= m_RecordList.count())
	{
		return -1;
	}
	
	if (bDeleteFlag)
	{
		if (OPER_ROW_INSERT == m_RecordList[RecordNo].nRowAttrib &&
			m_RecordList[RecordNo].bNewFlag)
		{
			char *pbuf = NULL;
			pbuf = m_RecordList[RecordNo].RecordBuff;
			if (pbuf)
			qDebug()<<"NAME=" << QString::fromLocal8Bit(((rDevS_Info*)pbuf)->Name) << ((rDevS_Info*)pbuf)->ParentId;
			m_RecordList.removeAt(RecordNo);
			m_RecordNum = m_RecordList.count();
			SetPageNum();
			bDelImmediately = true;
		}else
		{
			m_RecordList[RecordNo].nRowAttrib = OPER_ROW_DELETE;
			if (m_RecordList[RecordNo].UpDateFieldlist.count()>0)
			{
				m_RecordList[RecordNo].UpDateFieldlist.clear();
			}
		}
	}else
	{
		m_RecordList[RecordNo].nRowAttrib = OPER_ROW_NOOPERATE;
	}
	return 1;
}

//   判断当前记录是否有删除标志
bool MyTableData::IsCurRecordSetDeleteFlag(int RowNo)
{
	if (RowNo<0 || RowNo>=TABLE_PAGE_ROW_NUM)
	{
		return false;
	}
	int RecordNo = 0;
	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;

	if (!IsRecordParamSafe() || RecordNo<0 || RecordNo>= m_RecordList.count())
	{
		return false;
	}
	
	if (OPER_ROW_DELETE == m_RecordList[RecordNo].nRowAttrib)
	{
		return true;
	}else
	{
		return false;
	}
}

//   判断当前记录是否有新加记录标志
bool MyTableData::IsCurRecordNewInsertFlag(int RowNo)
{
	if (RowNo<0 || RowNo>=TABLE_PAGE_ROW_NUM)
	{
		return false;
	}
	int RecordNo = 0;
	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;

	if (!IsRecordParamSafe() || RecordNo<0 || RecordNo>= m_RecordList.count())
	{
		return false;
	}

	if (OPER_ROW_INSERT == m_RecordList[RecordNo].nRowAttrib)
	{
		return true;
	}else
	{
		return false;
	}
}

//	保存更改数据内容
int MyTableData::bSaveData()
{
	int RecordNum = m_RecordList.count();
	if (RecordNum<=0)
	{
		return 0;
	}
	unsigned int CurTime = QDateTime::currentDateTime().toTime_t();
	int	RecordNo = 0;
	g_pSqLiteDB.BeginTransaction();
	for (RecordNo = 0; RecordNo<RecordNum; RecordNo++)
	{
		if (RecordNo<0 && RecordNo>=m_RecordList.count())
		{
			break;
		}

		if (OPER_ROW_INSERT == m_RecordList[RecordNo].nRowAttrib &&
			m_RecordList[RecordNo].bNewFlag)
		{
			if (InsertNewRecord(&(m_RecordList[RecordNo])) <= 0)
			{
				g_pSqLiteDB.CommitTransaction();
				return -2;
				break;
			}
			

		}else if (OPER_ROW_UPDATE == m_RecordList[RecordNo].nRowAttrib)
		{
			if (UpDateOneRecord(&(m_RecordList[RecordNo])) < 0)
			{
				g_pSqLiteDB.CommitTransaction();
				return -3;
				break;
			}
			m_RecordList[RecordNo].UpDateFieldlist.clear();

		}else if (OPER_ROW_DELETE == m_RecordList[RecordNo].nRowAttrib)
		{
			if (!IsRecordEnableDelete(&(m_RecordList[RecordNo])))
			{
				//取消删除标志
				m_RecordList[RecordNo].nRowAttrib = OPER_ROW_NOOPERATE;
				return -4;
				break;
			}
			if (DeleteOneRecord(&(m_RecordList[RecordNo])) <= 0)
			{
				g_pSqLiteDB.CommitTransaction();
				return -5;
				break;
			}
			//else
			//{
			//	bDeleteFlag = true; 
			//}
		}

		//if (bDeleteFlag)
		//{
		//	m_RecordList.removeAt(RecordNo);
		//	RecordNo -= 1;
		//}
	}
	g_pSqLiteDB.CommitTransaction();

	return 1;

}

//添加一条新记录
int MyTableData::InsertNewRecord(RecordInfo *pRecord)
{
	if (pRecord && m_CurAppType>0 && m_CurTableId>0)
	{
		if (pRecord->bNewFlag &&
			OPER_ROW_INSERT == pRecord->nRowAttrib &&
			pRecord->RecordBuff && 
			pRecord->BuffLength>0)
		{
			unsigned int RecordId = 0;
			int  Result =0;
			int  TypeValue = 0;
			Result = g_DbCfgFunObj.GetNewFreeId(m_CurAppType, m_CurTableId, RecordId, TypeValue);
			if (RecordId<=0)
			{
				return -2;
			}

			for (int i=0; i<m_CurColumnNum; i++)
			{
				if (0 == QString(m_pColumnInfo[i].ColumnEngName).compare("ID", Qt::CaseInsensitive))
				{
					memcpy(pRecord->RecordBuff+m_pColBuffOffset[i], (char*)&RecordId, m_pColumnInfo[i].clmDataLength);
					break;
				}
			}

			if (!g_DbCfgFunObj.CheckDataIsSaft(m_CurAppType,m_CurTableId,(char*)pRecord->RecordBuff, OPER_ROW_INSERT))
			{
				return -3;
			}
			Result = g_DbCfgFunObj.InsertRecordIntoDB(m_CurAppType, m_CurTableId, pRecord->RecordBuff, pRecord->BuffLength, m_pColumnInfo, m_pColBuffOffset, m_CurColumnNum, m_CurTableInfo.TableEngName);
 			
			if (Result>0 && SYSTEM_TYPE_NSA_YD == m_CurAppType && TABLE_RTASK_INFO == m_CurTableId)
 			{
				g_DbCfgFunObj.AddTaskParaRecord(RecordId, ((rTask_Info*)pRecord->RecordBuff)->m_ChannelType,((rTask_Info*)pRecord->RecordBuff)->m_ProtType);
				/*
				char *buff=NULL;
				int ColumnNum=0;
				cColumn_Info *pColumnInfo=NULL;
				int *ColBuffOffset=NULL;

				rTask_Para_Info task_para_info_temp;
				
				memset(&task_para_info_temp,0x0,sizeof(rTask_Para_Info));
				task_para_info_temp.ParentId=RecordId+TABLE_RTASK_INFO*PARENT_OFFSET;

				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_PARA_INFO, task_para_info_temp.ID, 0);

 				int Ret = g_DbCfgFunObj.GetColumnInfobyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_PARA_INFO, buff, &ColumnNum);
				if (Ret>0)
				{
					pColumnInfo=new cColumn_Info[ColumnNum];

					ColBuffOffset = new int[ColumnNum];

					for (int i=0;i<ColumnNum;i++)
					{
						memcpy(pColumnInfo+i,(char*)(buff+sizeof(cColumn_Info)*i),sizeof(cColumn_Info));
					}

					for (int j=0;j<ColumnNum;j++)
					{
						int	BuffOffset = 0;
						if (j>0 && j<=ColumnNum)
						{
							for (int i=0; i<j; i++)
							{
								BuffOffset += pColumnInfo[i].clmDataLength;
							}
						}
						ColBuffOffset[j]=BuffOffset;
					}
					g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_PARA_INFO, (char*)&task_para_info_temp, sizeof(rTask_Para_Info), pColumnInfo, ColBuffOffset, ColumnNum);

					if (buff!=NULL)
					{
						delete buff;
						buff=NULL;
					}
					if (pColumnInfo!=NULL)
					{
						delete[] pColumnInfo;
						pColumnInfo=NULL;
					}
					if (ColBuffOffset!=NULL)
					{
						delete[] ColBuffOffset;
						ColBuffOffset=NULL;
					}
				}
				*/
 			}
			return Result;
		}
	}
	return -1;
}

//	删除一条新记录
int MyTableData::DeleteOneRecord(RecordInfo *pRecord)
{
	if (pRecord && m_CurAppType>0 && m_CurTableId>0)
	{
		if (OPER_ROW_DELETE == pRecord->nRowAttrib &&
			pRecord->RecordBuff && 
			pRecord->BuffLength>0)
		{
			unsigned int RecordId = 0;
			int  Result = GetCurRecordIdfromBuff(pRecord, RecordId);
			if (Result<=0 || RecordId<=0)
			{
				return -2;
			}
			if (SYSTEM_TYPE_NSA_YD == m_CurAppType)
			{
				switch (m_CurTableId)
				{
				case TABLE_RTASK_INFO:
					{
						DeleteTask(m_CurAppType, RecordId);
					}
					break;
				case TABLE_RDEVS_INFO:
					{
						DeleteDeviceChild(m_CurAppType, RecordId);
					}
					break;
				case TABLE_RDEVS_YX:
					{
						CheckAndClearYxRelate(RecordId);
					}
					break;
				case TABLE_RDEVS_YC:
					{
						CheckAndClearYcRelate(RecordId);
					}
					break;
				case TABLE_RDEVS_YK:
					{
						CheckAndClearYkRelate(RecordId);
					}
					break;
				case TABLE_REXPDEFINE_INFO:
					DeleteExp(m_CurAppType, RecordId);
					break;
				case TABLE_RDEVMODEL_INFO:
					{
						return  Del61850Model(m_CurAppType, m_CurTableId, RecordId);
					}
				case TABLE_RLOGIC_DEVICE:
					{
						DeleteLogicDeviceChild(SYSTEM_TYPE_NSA_YD,((rLogic_Device*)pRecord->RecordBuff)->ID);
					}
					break;
				case TABLE_RLOGIC_NODE:
					{
						DeleteLogicNodeChild(SYSTEM_TYPE_NSA_YD,((rLogic_Node*)pRecord->RecordBuff)->ID);
					}
					break;
				default:
					{
						return g_DbCfgFunObj.DeleteRecordByID(m_CurAppType, m_CurTableId, RecordId, m_CurTableInfo.TableEngName);
					}
					break;
				}
			}
			return g_DbCfgFunObj.DeleteRecordByID(m_CurAppType, m_CurTableId, RecordId, m_CurTableInfo.TableEngName);

		}
	}
	return 0;
}

//	修改一条新记录
int MyTableData::UpDateOneRecord(RecordInfo *pRecord)
{
	if (pRecord && m_CurAppType>0 && m_CurTableId>0)
	{
		if (OPER_ROW_UPDATE == pRecord->nRowAttrib && pRecord->RecordBuff && pRecord->BuffLength>0)
		{
			int	 UpFieldNum = 0;
			unsigned int RecordId = 0;
			char	 sql[SQL_MAX_LEN];
			char	 SetValSql[SQL_MAX_LEN];
			char	 *pFieldBuff = NULL;

			int	 ColumnId = 0;
			int	 ColumnNo = 0;
			QString	 SetValStr = 0;
			int	 i=0; 

			UpFieldNum = pRecord->UpDateFieldlist.count();
			if (UpFieldNum<=0)
			{
				pRecord->nRowAttrib = OPER_ROW_NOOPERATE;
				return 0;
			}
			int  Result = GetCurRecordIdfromBuff(pRecord,RecordId);
			if (Result<=0 || RecordId<=0)
			{
				return -2;
			}

			memset(sql, 0, SQL_MAX_LEN);
			memset(SetValSql, 0, SQL_MAX_LEN);
			if (!CheckUpDateRecordDataIsOk(pRecord))
			{
				return -5;
			}
			for (i=0; i<UpFieldNum; i++)
			{
				ColumnId = pRecord->UpDateFieldlist[i].ColumnId;
				ColumnNo = pRecord->UpDateFieldlist[i].ColumnNo;
				pFieldBuff = pRecord->UpDateFieldlist[i].pDataBuff;
				if (ColumnNo<0 || ColumnNo>=m_CurColumnNum || ColumnId<=0 || !pFieldBuff)
				{
					return -3;
					break;
				}

				if (SYSTEM_TYPE_NSA_YD == m_CurAppType &&
					TABLE_RDEVS_INFO == m_CurTableId &&
					0 == QString::fromLocal8Bit(m_pColumnInfo[ColumnNo].ColumnEngName).compare("Name", Qt::CaseInsensitive))
				{
					g_DbCfgFunObj.AdjustDevNameForSiYao(((rDevS_Info*)(pRecord->RecordBuff))->Name, pFieldBuff, ((rDevS_Info*)(pRecord->RecordBuff))->ID) ;
				}
				Result = g_DbCfgFunObj.GetUpdateColValueSQL(pFieldBuff, pRecord->UpDateFieldlist[i].Buff_Len, &(m_pColumnInfo[ColumnNo]),SetValStr);
				if (Result>0)
				{
					if (i<(UpFieldNum-1))
					{
						sprintf(SetValSql, "%s%s , ", SetValSql, SetValStr.toLocal8Bit().data());
					}else
					{
						sprintf(SetValSql, "%s%s", SetValSql, SetValStr.toLocal8Bit().data());
					}
				}else
				{
					return -4;
					break;
				}
			}

			sprintf(sql, "update %s set %s where ID=%d ", m_CurTableInfo.TableEngName, SetValSql, RecordId);
			Result = g_pSqLiteDB.DB_Modify(sql);
			return Result;
		}
	}
	return 0;
}

//	从当前记录的缓冲区中读取记录ID（用于delete Update）
int MyTableData::GetCurRecordIdfromBuff(RecordInfo *pRecord, unsigned int &RecordId)
{
	RecordId = 0;
	if (pRecord && m_CurAppType>0 && m_CurTableId>0)
	{
		if (!pRecord->RecordBuff || pRecord->BuffLength<=0)
		{
			return -1;
		}

		if (OPER_ROW_UPDATE == pRecord->nRowAttrib || OPER_ROW_DELETE == pRecord->nRowAttrib)
		{
			for (int i=0; i<m_CurColumnNum; i++)
			{
				if (0 == QString(m_pColumnInfo[i].ColumnEngName).compare("ID", Qt::CaseInsensitive))
				{
					memcpy((char*)&RecordId, pRecord->RecordBuff+m_pColBuffOffset[i], m_pColumnInfo[i].clmDataLength);
					return 1;
					break;
				}
			}
		}
	}
	return 0;
}


//	修改或添加操作前，先检查记录是否安全
bool MyTableData::IsRecordEnableDelete(RecordInfo *pRecord)
{
	if (!pRecord)
	{
		return false;
	}
	
	if (SYSTEM_TYPE_NSA_YD == m_CurAppType)
	{
		switch (m_CurTableId)
		{
		case TABLE_RTASK_INFO:
 			{
// 				if (((rTask_Info*)pRecord->RecordBuff)->LkCongenerId > 0 )
// 				{
// 					QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前任务[%1]设置了主备关系，不能删除！").arg(QString::fromLocal8Bit(((rTask_Info*)pRecord->RecordBuff)->Name)), QMessageBox::Ok | QMessageBox::Cancel);
// 					return false;
// 				}

				if (g_DbCfgFunObj.CurRecordIsHasChild(SYSTEM_TYPE_NSA_YD,TABLE_RTASK_INFO,((rTask_Info*)pRecord->RecordBuff)->ID,TABLE_RDEVS_INFO))
				{
					QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前任务[%1]配置有从属内容，不能删除！").arg(QString::fromLocal8Bit(((rTask_Info*)pRecord->RecordBuff)->Name)), QMessageBox::Ok | QMessageBox::Cancel);
					return false;
				}
			}
			break;
		case TABLE_RTRANSMITCONFIG_INFO:
			{
				if (g_DbCfgFunObj.CurRecordIsHasChild(SYSTEM_TYPE_NSA_YD,TABLE_RTRANSMITCONFIG_INFO,((rTransmitConfig_Info*)pRecord->RecordBuff)->ID,TABLE_RTRANSMIT_YX) ||
					g_DbCfgFunObj.CurRecordIsHasChild(SYSTEM_TYPE_NSA_YD,TABLE_RTRANSMITCONFIG_INFO,((rTransmitConfig_Info*)pRecord->RecordBuff)->ID,TABLE_RTRANSMIT_YC) ||
					g_DbCfgFunObj.CurRecordIsHasChild(SYSTEM_TYPE_NSA_YD,TABLE_RTRANSMITCONFIG_INFO,((rTransmitConfig_Info*)pRecord->RecordBuff)->ID,TABLE_RTRANSMIT_YK) ||
					g_DbCfgFunObj.CurRecordIsHasChild(SYSTEM_TYPE_NSA_YD,TABLE_RTRANSMITCONFIG_INFO,((rTransmitConfig_Info*)pRecord->RecordBuff)->ID,TABLE_RTRANSMIT_YM) )
				{
					QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前转发表[%1]配置有从属内容，不能删除！").arg(QString::fromLocal8Bit(((rTransmitConfig_Info*)pRecord->RecordBuff)->Name)), QMessageBox::Ok | QMessageBox::Cancel);
					return false;
				}
			}
			break;
			//如果是删除的逻辑设备
		case TABLE_RLOGIC_DEVICE:
			{
				if(g_DbCfgFunObj.CurRecordIsHasChild(SYSTEM_TYPE_NSA_YD,TABLE_RLOGIC_DEVICE,((rLogic_Device*)pRecord->RecordBuff)->ID,TABLE_RLOGIC_NODE))
				{
					int ret = QMessageBox::information(NULL,QString::fromLocal8Bit("数据库组态"),QString::fromLocal8Bit("当前设备表[%1]有从属内容，确定删除吗?").arg(QString::fromLocal8Bit(((rLogic_Device*)pRecord->RecordBuff)->Name)),QMessageBox::Ok |QMessageBox::Cancel);
					
						//删除逻辑节点和数据对象中关联记录
						//DeleteLogicDeviceChild(SYSTEM_TYPE_NSA_YD,((rLogic_Device*)pRecord->RecordBuff)->ID);
					if (ret == QMessageBox::Ok)
					{
						return true;
					}else
						return false;
					

				}

			}
			break;
		//如果删除的是逻辑节点
		case TABLE_RLOGIC_NODE:
			{
				if(g_DbCfgFunObj.CurRecordIsHasChild(SYSTEM_TYPE_NSA_YD,TABLE_RLOGIC_NODE,((rLogic_Node*)pRecord->RecordBuff)->ID,TABLE_RDATA_OBJECT))
				{
				   int ret =  QMessageBox::information(NULL,QString::fromLocal8Bit("数据库组态"),QString::fromLocal8Bit("当前逻辑节点表[%1:%2]有从属内容，确定删除吗?").arg(QString::fromLocal8Bit(((rLogic_Node*)pRecord->RecordBuff)->Name)).arg(QString::fromLocal8Bit(((rLogic_Node*)pRecord->RecordBuff)->Desc)),QMessageBox::Ok |QMessageBox::Cancel);
					
						//删除逻辑节点和数据对象中关联记录
						//DeleteLogicNodeChild(SYSTEM_TYPE_NSA_YD,((rLogic_Node*)pRecord->RecordBuff)->ID);
				   if (ret == QMessageBox::Ok)
					{
						return true;
					}else
						return false;
				}

			}
			break;
		default:
			{
				return true;
			}
			break;
		}
	}
	return true;
}

//删除逻辑节点关联
void MyTableData::DeleteLogicNodeChild(int AppType, unsigned int RecordId)
{
	char * Buff = NULL;
	//char * Buff_DO = NULL;
	int curLogicDeviceID = RecordId+TABLE_RLOGIC_NODE*PARENT_OFFSET; //即逻辑节点表记录的parentid
	int LogicNodeID = 0;
	int DataObjectID = 0;
	int buff_len = 0;
	int buff_len_DO = 0;
	int result = 0;
	//查找rlogic_Node中parentID是当前设备ID+偏移的项
	result =g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD,TABLE_RDATA_OBJECT,curLogicDeviceID,Buff,&buff_len,true);
	if (result <=0) return;
	for (int i= 0;i<result;i++)
	{
		LogicNodeID = ((rData_Object *)Buff)[i].ID;
		g_DbCfgFunObj.DeleteRecordByID(SYSTEM_TYPE_NSA_YD,TABLE_RDATA_OBJECT,LogicNodeID );
		/*buff_len = 0;
		g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD,TABLE_RDATA_OBJECT,LogicNodeID+TABLE_RLOGIC_NODE*PARENT_OFFSET);*/
		
		

	}
	

}



//删除逻辑设备关联：逻辑节点记录、数据对象记录
void MyTableData::DeleteLogicDeviceChild(int AppType, unsigned int RecordId)
{
	char * Buff = NULL;
	//char * Buff_DO = NULL;
	int curLogicDeviceID = RecordId+TABLE_RLOGIC_DEVICE*PARENT_OFFSET; //即逻辑节点表记录的parentid
	int LogicNodeID = 0;
	int DataObjectID = 0;
	int buff_len = 0;
	int buff_len_DO = 0;
	int result = 0;
	//查找rlogic_Node中parentID是当前设备ID+偏移的项
	result =g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD,TABLE_RLOGIC_NODE,curLogicDeviceID,Buff,&buff_len,true);
	if (result <=0) return;
	for (int i= 0;i<result;i++)
	{
		LogicNodeID = ((rLogic_Node *)Buff)[i].ID;
		if (((rLogic_Node *)Buff)[i].ParentId == curLogicDeviceID)
		{
			g_DbCfgFunObj.DeleteRecordByID(SYSTEM_TYPE_NSA_YD,TABLE_RLOGIC_NODE,LogicNodeID );
			buff_len = 0;
			g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD,TABLE_RDATA_OBJECT,LogicNodeID+TABLE_RLOGIC_NODE*PARENT_OFFSET);
		}

		
	}
	
}

//删除设备
int  MyTableData::DeleteDeviceChild(int AppType, unsigned int RecordId)
{
	if (SYSTEM_TYPE_NSA_YD != AppType || RecordId<=0)
	{
		return -1;
	}

	char	TableName[TABLE_ENG_NAME_LEN];
	int	Result = 0;
	unsigned int    ParentId;
	memset(TableName, 0, TABLE_ENG_NAME_LEN);
//	char  *Buff = NULL;
	
	ParentId = PARENT_OFFSET*TABLE_RDEVS_INFO+RecordId;
	
	ClearRelateYx(RecordId);
	sprintf(TableName, "rDevS_Yx");
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("遥信删除失败"));
	}
	ClearRelateYc(RecordId);
	sprintf(TableName, "rDevS_Yc");
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("遥测删除失败"));
	}
	
	ClearRelateYk(RecordId);
	sprintf(TableName, "rDevS_Yk");
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("遥控删除失败"));
	}
	
	sprintf(TableName, "rDevS_Ym");
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YM, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("遥脉删除失败"));
	}
	
	sprintf(TableName, "rDevS_BhYc");
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_BHYC, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("保护遥测删除失败"));
	}
	sprintf(TableName, "rDevS_SetS"); 
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_SETS, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("保护定值删除失败"));
	}
	sprintf(TableName, "rDevS_Param"); 
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_PARAM, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("装置参数删除失败"));
	}

	sprintf(TableName, "rDevS_JDXSS");  
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_JDXSS, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("精度系数删除失败"));
	}
	sprintf(TableName, "rDevS_Ryb");
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_RYB, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("软压板删除失败"));
	}
	
	sprintf(TableName, "rDevS_SetArea");
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_SETAREA, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("定值区删除失败"));
	}
	ClearDeviceRelateDevice(RecordId);
	return 1;
}

//删除任务
int  MyTableData::DeleteTask(int AppType, unsigned int RecordId)
{
	if (SYSTEM_TYPE_NSA_YD != AppType || RecordId<=0)
	{
		return -1;
	}

	char	TableName[TABLE_ENG_NAME_LEN];
	int	Result = 0;
	unsigned int    ParentId;
	memset(TableName, 0, TABLE_ENG_NAME_LEN);
	//	char  *Buff = NULL;

	ParentId = PARENT_OFFSET*TABLE_RTASK_INFO+RecordId;

	sprintf(TableName, "rTask_Para_Info");
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_PARA_INFO, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("任务参数删除失败"));
	}
	return 1;
}
int  MyTableData::DeleteExp(int AppType, unsigned int RecordId)
{
	if (SYSTEM_TYPE_NSA_YD != AppType || RecordId<=0)
	{
		return -1;
	}

	char	TableName[TABLE_ENG_NAME_LEN];
	int	Result = 0;
	unsigned int    ParentId;
	memset(TableName, 0, TABLE_ENG_NAME_LEN);
	//	char  *Buff = NULL;

	ParentId = PARENT_OFFSET*TABLE_REXPDEFINE_INFO+RecordId;

	sprintf(TableName, "rExpVariable_List");
	Result = g_DbCfgFunObj.DeleteRecordByParentId(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_PARA_INFO, ParentId, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("表达式变量删除失败"));
	}
	return 1;
}

int MyTableData::Del61850Model(int AppType, int TableId,unsigned int RecordID)
{
	if(SYSTEM_TYPE_NSA_YD != AppType || TableId != TABLE_RDEVMODEL_INFO || RecordID==0)return -1;

	char	sql[SQL_MAX_LEN];
	int	Result = 0;
	int    ParentId;
	memset(sql, 0, SQL_MAX_LEN);
	//	char  *Buff = NULL;

	ParentId = PARENT_OFFSET*TABLE_RDEVMODEL_INFO+RecordID;
	g_pSqLiteDB.BeginTransaction();
	sprintf(sql,"delete from rDevModel_Yx where ParentId=%d",ParentId);			//遥信模板
	Result = g_pSqLiteDB.DB_Modify(sql);	
	sprintf(sql,"delete from rDevModel_Yc where ParentId=%d",ParentId);			//遥测模板
	Result = g_pSqLiteDB.DB_Modify(sql);

	sprintf(sql,"delete from rDevModel_Yk where ParentId=%d",ParentId);			//遥控模板
	Result = g_pSqLiteDB.DB_Modify(sql);

	sprintf(sql,"delete from rDevModel_Ym where ParentId=%d",ParentId);			//遥脉模板
	Result = g_pSqLiteDB.DB_Modify(sql);

	sprintf(sql,"delete from rDevModel_Set where ParentId=%d",ParentId);		//定值模板 
	Result = g_pSqLiteDB.DB_Modify(sql);		

	sprintf(sql,"delete from rDevModel_Para where ParentId=%d",ParentId);		//参数模板 
	Result = g_pSqLiteDB.DB_Modify(sql);

	sprintf(sql,"delete from rDevModel_Prec where ParentId=%d",ParentId);		//参数模板 
	Result = g_pSqLiteDB.DB_Modify(sql);

	sprintf(sql,"delete from rDevModel_Bhyc where ParentId=%d",ParentId);		//参数模板 
	Result = g_pSqLiteDB.DB_Modify(sql);

	sprintf(sql,"delete from rDevModel_Ryb where ParentId=%d",ParentId);		//软压板模板 
	Result = g_pSqLiteDB.DB_Modify(sql);
	sprintf(sql,"delete from rDevModel_SetArea where ParentId=%d",ParentId);		//软压板模板 
	Result = g_pSqLiteDB.DB_Modify(sql);
	
	sprintf(sql,"delete from rDevModel_Info where ID=%d", RecordID);			//模板表
	g_pSqLiteDB.DB_Modify(sql);
	g_pSqLiteDB.CommitTransaction();
	return 1;
}


//清空遥信关联
int MyTableData::ClearRelateYx(unsigned int DeviceId)
{
	if (DeviceId<=0)
	{
		return 1;
	}
	char	*Yxbuff = NULL;
	int	buff_len = 0;
	int	RetVal = 0;
	int	i = 0; 
	RetVal = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO, Yxbuff, &buff_len, true);
	if (RetVal>0 && Yxbuff)
	{
		for (i=0; i<RetVal; i++)
		{
			CheckAndClearYxRelate(((rDevS_Yx *)Yxbuff)[i].ID);
		}
	}
	if (Yxbuff)
	{
		delete [](char*)Yxbuff;
		Yxbuff = NULL;
	}
	return 1;
}

//清空遥测关联
int MyTableData::ClearRelateYc(unsigned int DeviceId)
{
	if (DeviceId<=0)
	{
		return 1;
	}
	char	*Ycbuff = NULL;
	int	buff_len = 0;
	int	RetVal = 0;
	int	i = 0; 
	RetVal = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO, Ycbuff, &buff_len, true);
	if (RetVal>0 && Ycbuff)
	{
		for (i=0; i<RetVal; i++)
		{
			CheckAndClearYcRelate(((rDevS_Yc *)Ycbuff)[i].ID);
		}
	}
	if (Ycbuff)
	{
		delete [](char*)Ycbuff;
		Ycbuff = NULL;
	}
	return 1;
}

//清空遥控关联
int MyTableData::ClearRelateYk(unsigned int DeviceId)
{
	if (DeviceId<=0)
	{
		return 1;
	}
	char	*Ykbuff = NULL;
	int	buff_len = 0;
	int	RetVal = 0;
	int	i = 0; 
	RetVal = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO, Ykbuff, &buff_len, true);
	if (RetVal>0 && Ykbuff)
	{
		for (i=0; i<RetVal; i++)
		{
			CheckAndClearYkRelate(((rDevS_Yk *)Ykbuff)[i].ID);
		}
	}
	if (Ykbuff)
	{
		delete [](char*)Ykbuff;
		Ykbuff = NULL;
	}
	return 1;
}

//  检查并清空遥信记录的关联配置
void MyTableData::CheckAndClearYxRelate(unsigned int YxRecordId)
{
	if (YxRecordId<=0)
	{
		return ;
	}

	ClearDeviceRelateYx(YxRecordId);
	ClearYxRelateYx(YxRecordId);
	ClearYcRelateYx(YxRecordId);
	ClearYkRelateYx(YxRecordId);
	return ;
}

//  检查并清空遥测记录的关联配置
void MyTableData::CheckAndClearYcRelate(unsigned int YcRecordId)
{
	if (YcRecordId<=0)
	{
		return ;
	}

	return ;
}

//  检查并清空遥控记录的关联配置
void MyTableData::CheckAndClearYkRelate(unsigned int YkRecordId)
{
	if (YkRecordId<=0)
	{
		return ;
	}

	ClearYxRelateYk(YkRecordId);
	
	return ;
}

//   清空设备记录的关联设备
int MyTableData::ClearDeviceRelateDevice(unsigned int DeviceId)
{
	if (DeviceId<=0)
	{
		return 0;
	}
	QString sTableName;
	QString sColumnName;
	int	Ret = 0;
	unsigned int	RltDevId = 0;
	RltDevId = DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO;

	sTableName = QString::fromLocal8Bit("rDevS_Info");

	sColumnName = QString::fromLocal8Bit("devMapSeqDevId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, sTableName, sColumnName, RltDevId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空设备表关联字段[流程映射设备]失败！") );
		return Ret;
	}

	return 1;
}

//   清空设备记录的关联遥信
int MyTableData::ClearDeviceRelateYx(unsigned int YxId)
{
	if (YxId<=0)
	{
		return 0;
	}
	QString sTableName;
	QString sColumnName;
	int	Ret = 0;
	unsigned int	RltYxId = 0;
	RltYxId = YxId+PARENT_OFFSET*TABLE_RDEVS_YX;

	sTableName = QString::fromLocal8Bit("rDevS_Info");

	sColumnName = QString::fromLocal8Bit("devRepairYxId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, sTableName, sColumnName, RltYxId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空设备表关联字段[检修遥信]失败！") );
		return Ret;
	}

	return 1;
}

//   清空遥信记录的关联遥信
int MyTableData::ClearYxRelateYx(unsigned int YxId)
{
	if (YxId<=0)
	{
		return 0;
	}
	QString sTableName;
	QString sColumnName;
	int	Ret = 0;
	unsigned int	RltYxId = 0;
	RltYxId = YxId+PARENT_OFFSET*TABLE_RDEVS_YX;
	sTableName = QString::fromLocal8Bit("rDevS_Yx");
	sColumnName = QString::fromLocal8Bit("yxAccidentYxId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, sTableName, sColumnName, RltYxId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空遥信关联[事故信号]失败！") );
		return Ret;
	}

	sColumnName = QString::fromLocal8Bit("YxDoubleId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, sTableName, sColumnName, RltYxId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空遥信关联[副遥信点号]失败！") );
		return Ret;
	}
	return 1;
}

//   清空遥信记录的关联遥控
int MyTableData::ClearYxRelateYk(unsigned int YkId)
{
	if (YkId<=0)
	{
		return 0;
	}
	QString sTableName;
	QString sColumnName;
	int	Ret = 0;
	unsigned int	RltYkId = 0;
	RltYkId = YkId+PARENT_OFFSET*TABLE_RDEVS_YK;
	sTableName = QString::fromLocal8Bit("rYx_Info");
	sColumnName = QString::fromLocal8Bit("RelYkID");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, sTableName, sColumnName, RltYkId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空遥信关联[遥控]失败！") );
		return Ret;
	}

	sColumnName = QString::fromLocal8Bit("RelateSubYkId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, sTableName, sColumnName, RltYkId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空遥信关联[副遥控]失败！") );
		return Ret;
	}
	return 1;
}

//   清空遥测记录的关联遥信
int MyTableData::ClearYcRelateYx(unsigned int YxId)
{
	if (YxId<=0)
	{
		return 0;
	}
	QString sTableName;
	QString sColumnName;
	int	Ret = 0;
	unsigned int	RltYxId = 0;
	RltYxId = YxId+PARENT_OFFSET*TABLE_RDEVS_YX;
	
	sTableName = QString::fromLocal8Bit("rDevS_Yc");
	sColumnName = QString::fromLocal8Bit("ycSaltantYxId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, sTableName, sColumnName, RltYxId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空遥测关联[遥测突变告警遥信]失败！") );
		return Ret;
	}

	sColumnName = QString::fromLocal8Bit("ycStateYxId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, sTableName, sColumnName, RltYxId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空遥测关联[位置遥信点号]失败！") );
		return Ret;
	}
	sColumnName = QString::fromLocal8Bit("ycToZeroYxId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, sTableName, sColumnName, RltYxId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空遥测关联[归零遥信点号]失败！") );
		return Ret;
	}
	return 1;
}

//   清空遥控记录的所有关联遥信字段
int MyTableData::ClearYkRelateYx(unsigned int YxId)
{
	if (YxId<=0)
	{
		return 0;
	}
	QString sTableName;
	QString sColumnName;
	int	Ret = 0;
	unsigned int	RltYxId = 0;
	RltYxId = YxId+PARENT_OFFSET*TABLE_RDEVS_YX;
	
	sTableName = QString::fromLocal8Bit("rDevS_Yk");
	sColumnName = QString::fromLocal8Bit("StationEnaYxId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, sTableName, sColumnName, RltYxId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空遥控关联[站控使能遥信]失败！") );
		return Ret;
	}

	sColumnName = QString::fromLocal8Bit("WfLockYxId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, sTableName, sColumnName, RltYxId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空遥控关联[五防闭锁遥信]失败！") );
		return Ret;
	}
	sColumnName = QString::fromLocal8Bit("ykWfCloseYxId");
	Ret = g_DbCfgFunObj.ClearColumnData(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, sTableName, sColumnName, RltYxId);
	if (Ret < 0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("清空遥控关联[五防闭锁遥信]失败！") );
		return Ret;
	}
	return 1;
}

//	获取相关记录行号
int MyTableData::GetSibRecord(int CurRowNo, int &sibRowNo, int CurEditFieldNo)
{
	sibRowNo = -1;
	if (CurRowNo<0 || CurRowNo>=TABLE_PAGE_ROW_NUM)
	{
		return -1;
	}
	int RecordNo = 0;
	RecordNo = CurRowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;

	if (m_RecordList.count()<= 1)
	{
		return -1;
	}
	if (RecordNo<0 && RecordNo >= m_RecordList.count())
	{
		return -1;
	}

	int  SibPageNo = 0;
	unsigned int SibRecordId = 0;
	int  i = 0;
	
	return 0;
}

//	获取相关记录行号
int MyTableData::GetRelatedFieldNo(int CurEditFieldNo, int &RelatedFieldNo)
{
	RelatedFieldNo = -1;
	if (m_RecordList.count() < 1)
	{
		return -1;
	}
	int columnNo = 0;
	columnNo = GetFieldColumnNo(CurEditFieldNo);
	if (columnNo<0 || columnNo >= m_CurColumnNum)
	{
		return -2;
	}
	
	QString relatedColNameStr;

	if (relatedColNameStr.length()>0)
	{
		int  RelateColumnNo=-1;
		for (int i=0; i<m_CurShowFieldNum; i++)
		{
			RelateColumnNo = m_ShowFieidInfo[i].ColumnNo;
			if (RelateColumnNo>=0 && RelateColumnNo<m_CurColumnNum &&
				0 == QString::fromLocal8Bit(m_pColumnInfo[RelateColumnNo].ColumnEngName).compare(relatedColNameStr, Qt::CaseInsensitive) )
			{
				RelatedFieldNo = m_ShowFieidInfo[i].FieldNo;
				break;
			}
		}
		if (RelatedFieldNo>=0 && RelatedFieldNo<m_CurShowFieldNum)
		{
			return 1;
		}else
		{
			return 0;
		}
	}
	return 0;
}

//获取编码字符串
int MyTableData::GetNewCodeStr(int RowNo, int NewIndexNo, QString &NewCodeStr)
{
	if (m_RecordList.count() < 1 || RowNo<0 || RowNo>=m_RecordNum)
	{
		return -1;
	}
	/*char CodeBuff[STRING_NORMAL_LEN];
	char IndexBuff[CODE_NAME_LEN];
	int	CodeFieldNo=-1;
	QString CodeColNameStr;
//	int	i = 0;
	QString IndexStr;
	int	PreLen = 2;
	memset(CodeBuff, 0, STRING_NORMAL_LEN);
	memset(IndexBuff, 0, CODE_NAME_LEN);
	if (TABLE_GO_SUB_BAY == m_CurTableId)
	{
		CodeColNameStr = QString::fromLocal8Bit("bayCode");
		memcpy(CodeBuff, ((GO_Sub_Bay*)(m_RecordList.at(RowNo).RecordBuff))->bayCode, STRING_NORMAL_LEN);
		sprintf(IndexBuff, "%0.2d", NewIndexNo);
		IndexStr = QString::fromLocal8Bit(IndexBuff);
		PreLen = 2;
	}else if (TABLE_GO_PRIMA_DEVICE == m_CurTableId)
	{
		CodeColNameStr = QString::fromLocal8Bit("pdevCode");
		memcpy(CodeBuff, ((GO_Prima_Device*)(m_RecordList.at(RowNo).RecordBuff))->pdevCode, STRING_NORMAL_LEN);
		sprintf(IndexBuff, "%0.3d", NewIndexNo);
		IndexStr = QString::fromLocal8Bit(IndexBuff);
		PreLen = 3;
	}else if (TABLE_GO_MEASURE == m_CurTableId)
	{
		CodeColNameStr = QString::fromLocal8Bit("mCode");
		memcpy(CodeBuff, ((GO_Measure*)(m_RecordList.at(RowNo).RecordBuff))->mCode, STRING_NORMAL_LEN);
		sprintf(IndexBuff, "%0.1d", NewIndexNo);
		IndexStr = QString::fromLocal8Bit(IndexBuff);
		PreLen = 5;
	}

	if (CodeColNameStr.length()>0)
	{
		int  CodeColNo=-1;
		int  CodeColId = 0;
		int  tempNo=0;
		for (int i=0; i<m_CurShowFieldNum; i++)
		{
			tempNo = m_ShowFieidInfo[i].ColumnNo;
		
			if (tempNo>=0 && tempNo<m_CurColumnNum &&
				0 == QString::fromLocal8Bit(m_pColumnInfo[tempNo].ColumnEngName).compare(CodeColNameStr, Qt::CaseInsensitive) )
			{
				CodeFieldNo = m_ShowFieidInfo[i].FieldNo;
				CodeColId = m_ShowFieidInfo[i].ColumnId;
				CodeColNo = m_ShowFieidInfo[i].ColumnNo;
				break;
			}
		}
		int ChangIndex = FindChangDataFromList(RowNo, CodeFieldNo, CodeColId, CodeColNo);
		if (ChangIndex>=0)
		{
			memcpy(CodeBuff,m_RecordList[RowNo].UpDateFieldlist[ChangIndex].pDataBuff, m_RecordList[RowNo].UpDateFieldlist[ChangIndex].Buff_Len);
		}
	}
	if (QString::fromLocal8Bit(CodeBuff).length()>0)
	{
		NewCodeStr = QString::fromLocal8Bit(CodeBuff).left(PreLen)+IndexStr;
	}*/
	
	return 1;
}

//	是否有新数据未保存
bool MyTableData::IsNewDataUnSaved()
{
	int RecordCount = 0;
	RecordCount = m_RecordList.count();
	if (RecordCount>0)
	{
		for (int i=0; i<RecordCount; i++)
		{
			if (OPER_ROW_INSERT == m_RecordList.at(i).nRowAttrib ||
				OPER_ROW_DELETE == m_RecordList.at(i).nRowAttrib ||
				OPER_ROW_UPDATE == m_RecordList.at(i).nRowAttrib)
			{
				return true;
				break;
			}
		}
	}

	return false;
}

//	是否关联字段可编辑
bool MyTableData::IsRelateColumnEnableEdit(int RowNo, int FieldNo)
{
	if (RowNo<0 || RowNo>=TABLE_PAGE_ROW_NUM)
	{
		return false;
	}
	int RecordNo = 0;
	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;

	if (RecordNo<0 || FieldNo<0)
	{
		return false;
	}
	
	int RecordCount = 0;
	RecordCount = m_RecordList.count();
	if (RecordCount<=0 || RecordNo >= RecordCount)
	{
		return false;
	}
	if (!m_RecordList[RecordNo].RecordBuff)
	{
		return false;
	}

	/*if (TABLE_RNODE_INFO == m_CurTableId && SYSTEM_TYPE_NSA_YD == m_CurAppType)
	{
		if ((((rNode_Info*)(m_RecordList[RecordNo].RecordBuff))->ConfigState & NODE_STATE_MASTER_V) == 0 )
		{
			cColumn_Info ColInfo;
			GetFieldColumnInfo(FieldNo, ColInfo);
			if (0 == QString::fromLocal8Bit(ColInfo.ColumnEngName).compare(QString::fromLocal8Bit("ndCongenerId"),Qt::CaseSensitive) )
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点为从节点，不允许配置主从！"));
				return false;
			}else if (0 == QString::fromLocal8Bit(ColInfo.ColumnEngName).compare(QString::fromLocal8Bit("ndKind"),Qt::CaseSensitive) )
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点为从节点，不允许配置！"));
				return false;
			}
			else if (0 == QString::fromLocal8Bit(ColInfo.ColumnEngName).compare(QString::fromLocal8Bit("ConfigState"),Qt::CaseSensitive) )
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点为从节点，不允许配置！"));
				return false;
			}else if (0 == QString::fromLocal8Bit(ColInfo.ColumnEngName).compare(QString::fromLocal8Bit("ndNetMode"),Qt::CaseSensitive) )
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点为从节点，不允许配置！"));
				return false;
			}else if (0 == QString::fromLocal8Bit(ColInfo.ColumnEngName).compare(QString::fromLocal8Bit("ndIsInClosedSystem"),Qt::CaseSensitive) )
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点为从节点，不允许配置！"));
				return false;
			}
		
		}
	}else if (TABLE_RFRONTROUT_INFO == m_CurTableId && SYSTEM_TYPE_NSA_YD == m_CurAppType)
	{
		if (SYS_STATE_VALID != ((rFrontRout_Info*)(m_RecordList[RecordNo].RecordBuff))->frtRtConfigMaster)
		{
			cColumn_Info ColInfo;
			GetFieldColumnInfo(FieldNo, ColInfo);
			if (0 == QString::fromLocal8Bit(ColInfo.ColumnEngName).compare(QString::fromLocal8Bit("frtRtCongenerId"),Qt::CaseSensitive) )
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前通道为从通道，不允许配置主从！"));
				return false;
			}
		}
	}*/

	return true;
}


//	保存当前设备为模板设备
int MyTableData::SaveDeviceToModel(int RowNo)
{
	if (RowNo<0 || RowNo>=TABLE_PAGE_ROW_NUM)
	{
		return -1;
	}
	int RecordNo = 0;
	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;

	if (RecordNo>=0 && RecordNo<m_RecordNum &&
		IsRecordParamSafe() && 
		m_CurAppType == SYSTEM_TYPE_NSA_YD &&
		m_CurTableId == TABLE_RDEVS_INFO)
	{

		
	}
	return 0;
}

//  检查修改字段是否合理
// bool MyTableData::CheckUpDateColumnIsSafe(int RowNo, int FieldNo, char *NewDataBuff)
// {
// 	if (RowNo<0 || RowNo>=TABLE_PAGE_ROW_NUM)
// 	{
// 		return -1;
// 	}
// 	int RecordNo = 0;
// 	RecordNo = RowNo+m_CurPageNo*TABLE_PAGE_ROW_NUM;
// 
// 	if (RecordNo<0 || RecordNo>=m_RecordNum || 
// 		!IsRecordParamSafe() || 
// 		FieldNo<0 || FieldNo >= m_CurShowFieldNum ||
// 		m_CurAppType<=0 || m_CurTableId<=0)
// 	{
// 		return false;
// 	}
// 	int columnNo = 0;
// 	columnNo = GetFieldColumnNo(FieldNo);
// 	if (columnNo< columnNo>= m_CurColumnNum)
// 	{
// 		return false;
// 	}
// 
// 	if (m_CurAppType == SYSTEM_TYPE_NSA_YD && m_CurTableId == TABLE_RNODE_INFO)
// 	{
// 		unsigned int	RecordId = 0;
// 		GetCurRecordIdbyRowIndex(RecordNo, RecordId);
// 		if (QString::fromLocal8Bit(m_pColumnInfo[columnNo].ColumnEngName).compare("ndKind", Qt::CaseInsensitive) == 0 )
// 		{
// 			char ndKind = 0;
// 			ndKind = *((char*)NewDataBuff);
// 			if( (ndKind & NODE_KIND_SERVER) == 0 && g_DbCfgFunObj.CurNodeIsHasChild(RecordId, NODE_KIND_SERVER))
// 			{
// 				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点已经配置了服务器，不允许修改配置！"));
// 				return false;
// 			}
// 			if ( (ndKind & NODE_KIND_WORKSTATION)== 0 && g_DbCfgFunObj.CurNodeIsHasChild(RecordId, NODE_KIND_WORKSTATION) )
// 			{
// 				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点已经配置了工作站，不允许修改配置！"));
// 				return false;
// 			}
// 			if ( (ndKind&NODE_KIND_FRONT_SERVER)== 0 && g_DbCfgFunObj.CurNodeIsHasChild(RecordId, NODE_KIND_FRONT_SERVER) )
// 			{
// 				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("当前节点已经配置了前置机，不允许修改配置！"));
// 				return false;
// 			}
// 		}
// 	}
// 	return true;
// }

//设置当前页
int MyTableData::SetCurPageNo(int CurPageNo)
{
	if (CurPageNo>=0 && CurPageNo<m_PageNum) 
	{
		m_CurPageNo = CurPageNo;
	}else if (CurPageNo>=m_PageNum)
	{
		if (m_PageNum>0)
		{
			m_CurPageNo = m_PageNum-1;
		}else
		{
			m_CurPageNo = 0;
		}
	}
	return m_CurPageNo;
}
//设置下一页
int MyTableData::SetNextPage()
{
	if (m_PageNum>0 && m_CurPageNo<m_PageNum-1)
	{
		m_CurPageNo = (m_CurPageNo+1)%m_PageNum;
	}

	return m_CurPageNo;
}

//设置前一页
int MyTableData::SetPrePage()
{
	if (m_CurPageNo>0)
	{
		m_CurPageNo = (m_CurPageNo-1)%m_PageNum;
	}

	return m_CurPageNo;
}
int MyTableData::GetCurPageRecordNum()
{
	if (m_CurPageNo<0)
	{
		m_CurPageNo =0;
	}
	if (m_CurPageNo < m_PageNum-1)
	{
		return TABLE_PAGE_ROW_NUM;
	}else
	{
		return m_RecordNum-m_CurPageNo*TABLE_PAGE_ROW_NUM;
	}
}

void MyTableData::SetPageNum()
{
	if (m_RecordNum>0)
	{
		m_PageNum = (m_RecordNum+TABLE_PAGE_ROW_NUM-1)/TABLE_PAGE_ROW_NUM;
	}else
	{
		m_PageNum =0;
	}
}

//检查当前修改是否合理
bool MyTableData::CheckUpDateRecordDataIsOk(RecordInfo *pRecord)
{
	if (!pRecord)
	{
		return false;
	}
	bool Result = false;
	if (pRecord->BuffLength>0 && pRecord->UpDateFieldlist.count()>0)
	{
		char *pTempBuff = NULL;
		pTempBuff = new char[pRecord->BuffLength];
		memset(pTempBuff, 0, pRecord->BuffLength);
		memcpy(pTempBuff, pRecord->RecordBuff, pRecord->BuffLength);
		int UpLstCount = pRecord->UpDateFieldlist.count();
		int Bufflen = 0;
		int DataOffset = 0;
		char *pFDataBuff=NULL;
		for (int i=0; i<UpLstCount; i++)
		{
			Bufflen = pRecord->UpDateFieldlist[i].Buff_Len;
			pFDataBuff = pRecord->UpDateFieldlist[i].pDataBuff;
			DataOffset = GetFieldDataOffset(pRecord->UpDateFieldlist[i].FieldIndex);
			if (DataOffset>=0 && DataOffset<=(pRecord->BuffLength-Bufflen))
			{
				memcpy(pTempBuff+DataOffset, pFDataBuff, Bufflen);
			}
		}
		Result = g_DbCfgFunObj.CheckDataIsSaft(m_CurAppType,m_CurTableId,pTempBuff,OPER_ROW_UPDATE);
		delete [](char*)pTempBuff;
		pTempBuff = NULL;
	}
	
	return Result;
}



int MyTableData::SetCurSelectRecordNo(int CurSeltRecordNo)
{
	if (CurSeltRecordNo>=0 && 
		IsRecordParamSafe() &&
		CurSeltRecordNo<m_RecordList.count()) 
	{
		m_SeltRecordNo = CurSeltRecordNo;
		return 1;
	}else
	{
		m_SeltRecordNo = -1;
		return -1;
	}
};
